package com.yoo.module_base.utils

import android.app.Activity
import android.graphics.drawable.Drawable
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.ImageSpan
import com.yalantis.ucrop.util.DensityUtil

class ImageDrawableUtil {
    fun addIconsToText(activity: Activity,text: String, icon: Drawable?): SpannableStringBuilder {
        val spannableStringBuilder = SpannableStringBuilder(text)

        // 设置图标的宽高
        val iconWidth = ScreenUtils.dip2px(activity,22f)
        val iconHeight = ScreenUtils.dip2px(activity,22f)
        if (icon!= null) {
            icon.setBounds(0, 0, iconWidth, iconHeight)
            val imageSpan1 = ImageSpan(icon, ImageSpan.ALIGN_BOTTOM)
            spannableStringBuilder.append("  ")
            spannableStringBuilder.setSpan(imageSpan1, spannableStringBuilder.length - 1, spannableStringBuilder.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        }
        return spannableStringBuilder
    }


    fun addIconsToText(activity: Activity,text: String, icon1: Drawable?, icon2: Drawable): SpannableStringBuilder {
        val spannableStringBuilder = SpannableStringBuilder(text)

        // 设置图标的宽高
        val iconWidth = DensityUtil.dip2px(activity,23f)
        val iconHeight = DensityUtil.dip2px(activity,23f)
        if (icon1!= null) {
            icon1.setBounds(0, 0, iconWidth, iconHeight)
            val imageSpan1 = ImageSpan(icon1, ImageSpan.ALIGN_BOTTOM)
            spannableStringBuilder.append("  ")
            spannableStringBuilder.setSpan(imageSpan1, spannableStringBuilder.length - 1, spannableStringBuilder.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        }
        icon2.setBounds(0, 0, iconWidth, iconHeight)
        val imageSpan2 = ImageSpan(icon2, ImageSpan.ALIGN_BOTTOM)
        spannableStringBuilder.append("  ")
        spannableStringBuilder.setSpan(imageSpan2, spannableStringBuilder.length - 1, spannableStringBuilder.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

        return spannableStringBuilder
    }

}